class NguoiDungController < ApplicationController
  include ApplicationHelper
  def index
  end

  def lich_su_mua_hang
    if is_khach
      redirect_to root_url
    return
    end
    @nguoi_dung = nguoi_dung_hien_tai
    @trang = (params[:page].present? && params[:page].to_i > 1 ?  params[:page] : 1).to_i
    query = GioHang.where(:nguoi_dung_id => @nguoi_dung.id, :da_xac_nhan => true)
    @gio_hangs = query.paginate(
      :order => :updated_at.desc,
      :per_page => @so_sp_moi_trang,
      :page => @trang
    )
    @co_trang_sau = !query.paginate(
      :order => :updated_at.desc,
      :per_page => @so_sp_moi_trang,
      :page => @trang + 1
    ).empty?

    # @san_phams = @san_phams + @san_phams + @san_phams + @san_phams + @san_phams + @san_phams
    @co_trang_truoc = @trang > 1
    @index_dau_tien = (@trang - 1) * 20
  end

  def cap_nhat
    if is_khach
      redirect_to root
    end

    if params[:commit_normal].present?
      nguoi_dung = nguoi_dung_hien_tai
      nguoi_dung.gioi_tinh = params[:gioi_tinh]
      nguoi_dung.ten_hien_thi = params[:ten_hien_thi]
      nguoi_dung.ngon_ngu = params[:ngon_ngu]
      nguoi_dung.so_dien_thoai = Phoner::Phone.parse params[:so_dien_thoai]
      begin
        ngay_sinh = DateTime.new(params[:ngay_sinh]['nam'].to_i,params[:ngay_sinh]['thang'].to_i,params[:ngay_sinh]['ngay'].to_i)
        nguoi_dung.ngay_sinh = ngay_sinh
        if !nguoi_dung.save
          flash.now[:loi] = t 'kiem_tra_lai_cac_thong_tin_da_nhap'
        else
          flash.now[:thanh_cong] = t 'ho_so_cap_nhat_thanh_cong'
        end
      rescue
        flash.now[:loi] = t 'ngay_sinh_khong_hop_le'
      end

    end
    if params[:commit_pri].present?
      nguoi_dung = nguoi_dung_hien_tai
      if nguoi_dung.kiem_tra_mat_khau(params[:mat_khau_hien_tai].strip)
        nguoi_dung.email = params[:email]
        if params[:mat_khau].present?
          if params[:mat_khau] != params[:mat_khau_confirmation]
            flash.now[:loi] = t 'mat_khau_moi_phai_duoc_nhap_2_lan'
          else
            nguoi_dung.mat_khau_hash = Digest::SHA1.hexdigest(params[:mat_khau].strip)
          end
        end
        if !nguoi_dung.save
          flash.now[:loi] = t 'kiem_tra_lai_cac_thong_tin_da_nhap'
        else
          flash.now[:thanh_cong] = t 'ho_so_cap_nhat_thanh_cong'
        end
      else
        flash.now[:loi] = t 'mat_khau_hien_tai_khong_dung'
      end

    end
    @nguoi_dung = nguoi_dung_hien_tai

  end

  def quen_mat_khau
    if params[:commit].present?
      if (params[:email].present?)
        user = NguoiDung.quen_mat_khau(params[:email], @hostname)
        if (user.present?)
          flash.now[:thanh_cong] = t('da_gui_email_khoi_phuc_mat_khau')
        else
          flash.now[:loi] = t('khong_tim_thay_email_nay_hoac_email_khong_hop_le')
        end

      end
    end
  end

  def tao_mat_khau
    @request_url = request.url
    if params[:token].present? && params[:commit].present?
      if NguoiDung.find_by_mat_khau_moi_token(params[:token]).present?
        user = NguoiDung.tao_mat_khau_moi(params[:token], params[:mat_khau], params[:mat_khau_confirmation])
        if user
          flash.now[:thanh_cong] = t('mat_khau_da_duoc_tao_lai')
        else
          flash.now[:loi] = t('mat_khau_phai_dai_hon_6_ki_tu_va_giong_nhau')
        end
      else
        flash.now[:loi] = t('lien_ket_khoi_phuc_mat_khau_khong_con_hieu_luc')
      end

    end
  end
end
